package com.ruoyi.mysystem.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import org.springframework.stereotype.Component;

@Component
public class AliPayUtil {
    AlipayClient alipayClient = new DefaultAlipayClient(
            PaymentConstants.URL,
            PaymentConstants.APPID,
            PaymentConstants.PRIVATE_KEY,
            PaymentConstants.FORMAT,
            PaymentConstants.CHARSET,
            PaymentConstants.ALIPAY_PUBLIC_KEY,
            PaymentConstants.SIGN_TYPE);

    /**
     * 创建预支付界面接口
     *
     * @param money       支付金额
     * @param orderNumber 商家订单号
     * @return String
     * @throws AlipayApiException
     */
    public String getAlipayView(Double money, String orderNumber) throws AlipayApiException {
//public static void main(String[] args) throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl(PaymentConstants.NOTIFY_URL);
//同步跳转地址，仅支持http/https
//        request.setReturnUrl("https://4z393v9628.zicp.fun/main");
/******必传参数******/
        JSONObject bizContent = new JSONObject();
//商户订单号，商家自定义，保持唯一性 "20210817010101005"
        bizContent.put("out_trade_no", orderNumber);
//支付金额，最小值0.01元
        bizContent.put("total_amount", money);
//订单标题，不可使用特殊符号
        bizContent.put("subject", "测试商品");
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

/******可选参数******/
//bizContent.put("time_expire", "2022-08-01 22:00:00");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
// 如果需要返回GET请求，请使用
// AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return pageRedirectionData;
    }

    /**
     * 查询订单状态接口
     *
     * @param outTradeNo 商家订单号
     * @return AlipayTradeQueryResponse
     * @throws AlipayApiException
     */
    public AlipayTradeQueryResponse query(String outTradeNo) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{\"out_trade_no\": " + outTradeNo + "}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
            return null;
        }
    }

    /**
     * 关闭订单接口
     *
     * @param tradeNo
     * @return AlipayTradeCloseResponse
     * @throws AlipayApiException
     */
    public AlipayTradeCloseResponse close(String tradeNo) throws AlipayApiException {
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", tradeNo);
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
            return null;
        }
    }

    /**
     * 退款接口
     *
     * @param tradeNo      支付宝交易号
     * @param outRequestNo 退款订单号
     * @return AlipayTradeRefundResponse
     * @throws AlipayApiException
     */
    public AlipayTradeRefundResponse refund(String tradeNo, String outRequestNo) throws AlipayApiException {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", tradeNo);
//        bizContent.put("refund_amount", 0.01);
        bizContent.put("out_request_no", outRequestNo);

//// 返回参数选项，按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
            return null;
        }
    }

    /**
     * 查询退款订单接口
     *
     * @param outRequestNo 商家退款订单号
     * @return AlipayTradeFastpayRefundQueryResponse
     * @throws AlipayApiException
     */
    public AlipayTradeFastpayRefundQueryResponse queryRefund(String outRequestNo) throws AlipayApiException {
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        JSONObject bizContent = new JSONObject();
//        bizContent.put("trade_no", "2021081722001419121412730660");
        bizContent.put("out_request_no", outRequestNo);

//// 返回参数选项，按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
            return null;
        }
    }

}
